using System;
using System.Collections.Generic;
using System.Text;

namespace GraphFS
{
	public class FSNodesCache : Dictionary<Int64, FSNode>
	{
		private int maxValues = 1000;
		private List<Int64> nodeIds = new List<long>();

		public FSNodesCache():base()
		{

		}
		public FSNodesCache(int maxVals):base()
		{
			maxValues = maxVals;
		}

		public new void Add(Int64 key, FSNode value)
		{
			nodeIds.Add(key);
			base.Add(key, value);
			Cleanup();
		}

		private void Cleanup()
		{
			if (nodeIds.Count > 2 * maxValues)
			{
				int nrElemsToRemove = nodeIds.Count - maxValues;
				for (int i = 0; i < nrElemsToRemove; i++)
				{
					this.Remove(nodeIds [i]);
				}
				nodeIds.RemoveRange(0, nrElemsToRemove);
			}
		}

	}
}
